//
//  AlertViewController.swift
//  DemoProject
//
//  Created by hao on 2018/7/18.
//  Copyright © 2018年 hao. All rights reserved.
//

import UIKit

class AlertViewController: BaseTableViewController {

    lazy var showAlertBtn: UIButton = {
        let _btn = UIButton(type: .system)
        self.tableHeadView.addSubview(_btn)
        _btn.snp.makeConstraints({ (make) in
            make.center.equalTo(self.tableHeadView)
        })
        _btn.setTitle("弹出Alert", for: .normal)
        _btn.addTarget(self, action: #selector(showAlertClick), for: .touchUpInside)
        return _btn
    }()
    
    lazy var showSheetBtn: UIButton = {
        let _btn = UIButton(type: .system)
        self.tableHeadView.addSubview(_btn)
        _btn.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.top.equalTo(showAlertBtn.snp.bottom).offset(20)
        })
        _btn.addTarget(self, action: #selector(showSheetClick), for: .touchUpInside)
        return _btn
    }()
    
    lazy var showDatePickerBtn: UIButton = {
        let _btn = UIButton(type: .system)
        self.tableHeadView.addSubview(_btn)
        _btn.snp.makeConstraints({ (make) in
            make.centerX.equalTo(self.tableHeadView)
            make.bottom.equalTo(showAlertBtn.snp.top).offset(-20)
        })
        _btn.setTitle("弹出DatePicker", for: .normal)
        return _btn
    }()
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupData()
        showSheetBtn.setTitle("弹出Sheet", for: .normal)
        // 弹出时间选择器
        showDatePickerBtn.rx.controlEvent(.touchUpInside).subscribe(onNext: { // [weak self] in
            let sheetVC = UIAlertController(title: "\n\n\n\n\n\n\n\n\n\n\n", message: nil, preferredStyle: .actionSheet)
            let height = MainScreenHeight()/3
            let datePicker = UIDatePicker(frame: CGRect(x: 0, y: 0, width: MainScreenWidth(), height: height))
            sheetVC.view.addSubview(datePicker)
            datePicker.locale = Locale(identifier: "zh_CN")
            datePicker.datePickerMode = .date
            
            let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            sheetVC.addAction(cancelAction)
            // 设置UIAlertAction按钮标题的颜色
            cancelAction.setValue(UIColor.red, forKey: "titleTextColor")
            self.present(sheetVC, animated: true, completion: nil)
        }).disposed(by: disposeBag)
    }
    
    @objc private func showSheetClick() {
        let sheetVC = UIAlertController(title: "提示sheet", message: "", preferredStyle: .actionSheet)
        let action1 = UIAlertAction(title: "第1个", style: .default, handler: nil)
        let action2 = UIAlertAction(title: "第2个", style: .destructive, handler: nil)
        let action3 = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        sheetVC.addAction(action1)
        sheetVC.addAction(action2)
        sheetVC.addAction(action3)
        self.present(sheetVC, animated: true, completion: nil)
    }
    
    @objc private func showAlertClick() {
        let alertVC = UIAlertController(title: "", message: "", preferredStyle: .alert)
        let attStr = NSAttributedString(string: "提示框", attributes: [NSAttributedStringKey.foregroundColor : UIColor.red,
                                                                 NSAttributedStringKey.font : UIFont.systemFont(ofSize: 22)])
        alertVC .setValue(attStr, forKey: "attributedTitle")
        
        let attStr2 = NSAttributedString(string: "提示的信息内容", attributes: [NSAttributedStringKey.foregroundColor : UIColor.blue])
        alertVC .setValue(attStr2, forKey: "attributedMessage")
        
        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        alertVC.addAction(cancelAction)
        self.present(alertVC, animated: true, completion: nil)
    }
    
    private func setupData() {
        let titles = ["修改title 和Message属性",
                      "提示框样式",
                      "提示框上加UIDatepicker"]
        
        let descArr = ["""
                        [alertController setValue:alertControllerStr forKey:@"attributedTitle"];
                        [alertController setValue:alertControllerMessageStr forKey:@"attributedMessage"];
                       """,
                       """
                        UIAlertControllerStyle.actionSheet
                        UIAlertControllerStyle.alert
                       """,
                       """
                        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                        UIDatePicker *datePicker=[[UIDatePicker alloc] init];
                        datePicker.datePickerMode=UIDatePickerModeDate;
                        NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];// 设置日期选择控件的地区
                        datePicker.locale = locale;
                        [alertVC.view addSubview:datePicker];
                        datePicker.frame = CGRectMake();
                        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
                        [cancelAction setValue:[UIColor redColor] forKey:@"titleTextColor"];
                        [alertVC addAction:cancelAction];
                        [self presentViewController:alertVC animated:YES completion:nil];
                       """]
        
        addTitles(titles, footDescs: descArr)
    }

}
